package com.nari.ncps.screen.service.impl.ne;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.ne.NeProdoperPowerGs;
import com.nari.ncps.screen.entity.ne.NeProdoperPowerTotal;
import com.nari.ncps.screen.mapper.ne.NeProdoperSalesIncomeMapper;
import com.nari.ncps.screen.service.ne.NeProdoperSalesIncomeService;
import com.nari.ncps.screen.vo.ne.NePowerGenerationAnnualVO;
import com.nari.ncps.screen.vo.ne.NeSalesIncomeVO;
import com.nari.ncps.screen.entity.ne.NeProdoperSalesIncome;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 新能源运营营收情况表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeProdoperSalesIncomeServiceImpl extends ServiceImpl<NeProdoperSalesIncomeMapper, NeProdoperSalesIncome> implements NeProdoperSalesIncomeService {

    @Autowired
    private NeProdoperSalesIncomeMapper neProdoperSalesIncomeMapper;

    @Override
    public List<NeSalesIncomeVO> getSalesIncome(Integer year) {
        // 1. 确定查询的年份范围
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);

        // 如果指定了年份，查询该年份及之前4年；否则查询最近5年
        int endYear = (year != null) ? year : currentYear;
        int startYear = endYear - 4;
        LambdaQueryWrapper<NeProdoperSalesIncome> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NeProdoperSalesIncome::getOrgCode, "100360")//根据orgcode来查询是哪个公司的，如果没有传orgcode那么就会按照内蒙公司的维度进行查询
                .eq(NeProdoperSalesIncome::getDateType, 1);
        queryWrapper.ge(NeProdoperSalesIncome::getYear, startYear)
                .le(NeProdoperSalesIncome::getYear, endYear)
                .orderByAsc(NeProdoperSalesIncome::getYear);
        List<NeProdoperSalesIncome> allData = this.list(queryWrapper);
        // 循环遍历allData，将相同year的plannedGeneration和actualGeneration相加
        Map<Integer, NeProdoperSalesIncome> yearMap = new HashMap<>();
        for (NeProdoperSalesIncome data : allData) {
            Integer yearKey = data.getYear();
                if (yearMap.containsKey(yearKey)) {
                    // 如果已存在该年份，则累加数值
                    NeProdoperSalesIncome existing = yearMap.get(yearKey);
                    if(data.getTotalRevenue()!=null&&data.getTotalRevenue().compareTo(BigDecimal.ZERO)>0){
                        if (existing != null && existing.getTotalRevenue() != null
                                && data.getTotalRevenue() != null && data.getTotalRevenue().compareTo(BigDecimal.ZERO)>0
                                && data.getTotalRevenue().compareTo(existing.getTotalRevenue()) > 0 &&
                                data.getTotalRevenue().compareTo(BigDecimal.ZERO)>0) {
                            existing.setTotalRevenue(existing.getTotalRevenue().add(data.getTotalRevenue()));
                        }
                    }
                    if(data.getOperatingProfit()!=null&&data.getOperatingProfit().compareTo(BigDecimal.ZERO)>0){
                        if(existing != null && existing.getOperatingProfit() != null
                                && data.getOperatingProfit() != null
                                && data.getOperatingProfit().compareTo(BigDecimal.ZERO)>0
                                && data.getOperatingProfit().compareTo(existing.getOperatingProfit()) > 0){
                            existing.setOperatingProfit(existing.getOperatingProfit().add(data.getOperatingProfit()));
                        }
                    }
                } else {
                    // 如果不存在，则添加到map中
                    yearMap.put(yearKey, data);
                }
        }
        List<NeSalesIncomeVO> resultList = new ArrayList<>();
        yearMap.forEach((newYear, data) -> {
            NeSalesIncomeVO vo = new NeSalesIncomeVO();
            vo.setYear(newYear);
            if(data.getTotalRevenue()!=null&&data.getTotalRevenue().compareTo(BigDecimal.ZERO)>0){
                vo.setTotalRevenue(data.getTotalRevenue());
            }else {
                vo.setTotalRevenue(BigDecimal.ZERO);
            }
            if (data.getOperatingProfit()!=null&&data.getOperatingProfit().compareTo(BigDecimal.ZERO)>0){
                vo.setOperatingProfit(data.getOperatingProfit());
            }else{
                vo.setOperatingProfit(BigDecimal.ZERO);
            }
            resultList.add(vo);
        });
        //        升序
        resultList.sort(Comparator.comparing(NeSalesIncomeVO::getYear));
        return resultList;
    }
}
